<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
	xmlns:cs="http://myfaces.apress.com/custom"
	template="/WEB-INF/layouts/standard.xhtml" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"
	xmlns:bf="http://www.bssmanager.com/jsf" 
	 xmlns:fn="http://www.bss.com/functions"
	 xmlns:c="http://java.sun.com/jstl/core">
	 
	 
	 <ui:define name="headIncludes">
		<style>
			
			td {
				vertical-align: top;
			}
			
			
		</style>
	</ui:define>

	<ui:define name="formheader">
		<h2>Payments</h2>
	</ui:define>
	
	<ui:define name="formGlobal">
		<bf:dateSelector controller="#{admissionController}"  date="date" ajaxUpdate="false"/>
	</ui:define>
		
	<ui:define name="content">	
		<h:form name="paymentForm" prependId="false">
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Select Patient"/>
				</f:facet>
				<h:panelGroup rendered="#{paymentController.currentProcess eq 'undefined'}">
							<bf:admissionSelector controller="#{paymentController}" selectMethod="selectAdmission" reRender="selectedPatient,tabPayments,dtRcpts"/>
						</h:panelGroup>			
			</rich:panel>
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Patient Details"/>
				</f:facet>
				<h:panelGroup id="selectedPatient" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }">
						<h:outputText
							value="#{paymentController.admission.dateOfExit == null ? '': 'Patient has Checked Out !'} #{paymentController.admission.dateOfExit == null ? '': messages['payment.error.not-allowed']}"
							style="font-weight:bold; color:red" />
						<h:panelGrid columns="2">
							<h:outputLabel value="Name: " />
							<h:outputText value="#{paymentController.admission.patient.name} (#{paymentController.admission.id})" />
							<h:outputLabel value="Checked In: " />
							<h:outputText value="#{paymentController.admission.dateOfAdmission}">
								<f:convertDateTime pattern="dd MMM yyyy HH:MM" />
							</h:outputText>								
							<h:outputLabel rendered="#{paymentController.currentProcess eq 'deposit'}" value="Utensil Deposit Required"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'deposit'}" style="color:red" value="Rs #{paymentController.admission.materialSet.deposit} "/>
							<h:outputText value="Utensil Deposit done"/>
							<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getUtensilDepositDone', paymentController.receipts)}"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" value="Utensil Deposit Refund done"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" style="color:red" value="Rs #{fn:call(paymentController,'getUtensilRefundDone', paymentController.receipts)}"/>
							<h:outputText value="Security Deposit done"/>
							<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getSecurityDepositDone', paymentController.receipts)}"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" value="Security Deposit Refund done"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" style="color:red" value="Rs #{fn:call(paymentController,'getSecurityRefundDone', paymentController.receipts)}"/>
							<h:outputLabel value="Total Deposit received" />
							<h:outputText value="Rs #{fn:call(paymentController,'calculateReceiptTotalDeposit', paymentController.receipts)}"/>
							<h:outputLabel rendered="#{paymentController.currentProcess eq 'refund'}" value="Total Refund done" />
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" value="Rs #{fn:call(paymentController,'calculateReceiptTotalRefund', paymentController.receipts)}"/>							
						</h:panelGrid>						
					</h:panelGroup>		
			</rich:panel>		
			<a4j:commandButton reRender="depositSummaryPanel" 
							value="Complete deposit and PRINT ID"
							action="#{paymentController.initReceipts}"
							style="display: #{paymentController.currentProcess eq 'deposit' ? 'block':'none'}"
							onclick="showLoading();"
							limitToList="true"
							oncomplete="hideLoading();Richfaces.showModalPanel('depositSummaryPanel');"/>
								
					<a4j:commandButton reRender="refundSummaryPanel" value="Complete refund and CHECK OUT "
							action="#{paymentController.initReceipts}"
							style="display:#{paymentController.currentProcess eq 'refund' ? 'block':'none'}" 
							onclick="showLoading();"
							limitToList="true"
							oncomplete="hideLoading();Richfaces.showModalPanel('refundSummaryPanel');"/>	
							
							
							
			<rich:panel style="border:0">
				<f:facet name="header">
					<h:outputText value="Receipt Details"/>
				</f:facet>
				<h:panelGroup id="receiptInfo">
						<h:panelGrid columns="1" rendered="#{ empty paymentController.selectedReceipt }">							
							<h:outputText rendered="#{paymentController.currentProcess eq 'deposit'}" value="Use below tabs to perform deposits. You will automatically be redirected to ID Generation page after all deposits are done. To skip deposit process and move immediately, use the above button 'Complete deposit and PRINT ID'" style="color:red; font-weight: bolder"/>
							<h:outputText rendered="#{paymentController.currentProcess eq 'refund'}" value="Use below tabs to perform refunds. You will automatically be redirected to Checkout Summary Page after all refunds are done. To skip deposit process and move immediately, use above button 'Complete refund and CHECK OUT' " style="color:red; font-weight: bolder"/>
						</h:panelGrid>
						<h:panelGrid columns="2" rendered="#{ not empty paymentController.selectedReceipt }">
							<h:outputLabel value="Receipt ID: " />
									<h:outputText value="#{paymentController.selectedReceipt.id}"/>													
									<h:outputLabel value="Remarks: " />						
									<h:outputText value="#{paymentController.selectedReceipt.remarks}"/>
									<h:outputLabel value="Created Date: " />
									<h:outputText value="#{paymentController.selectedReceipt.date}">
										<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
									</h:outputText>
						</h:panelGrid>						
					</h:panelGroup>
			</rich:panel>
			
			<h:panelGrid columns="2" id="tabPayments" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }; width:90%;">
				<rich:panel id="tabSecDep" rendered="#{paymentController.accessFinder['Security Deposit'] }" label="Security Deposit" switchType="client"  >
					
				
								<h:panelGrid columns="2">								

											<h:panelGrid columns="2" styleClass="noprint" id="secDepGrid"
												rendered="#{paymentController.visibilityFinder['Security Deposit,deposit'] or paymentController.visibilityFinder['Security Deposit,refund']}">
												<h:outputLabel value="Amount: "/>
												<h:inputText 	 
																onkeyup="validateInteger(this,true,'Only numbers allowed !');" 
																size="4" id="txtSecDep"
																value="#{paymentController.securityDepositAmount}"
																title="Amount must be a number greater than 0">
												</h:inputText>
											
												<a4j:commandButton onclick="if(document.getElementById('txtSecDep').value == 0) { alert('Amount is not valid !'); return false;} showLoading(); " 
																value="Deposit"
																id="btnSecDep" limitToList="true"
																action="#{paymentController.securityDeposit}" 
																reRender="dtSecurityDeposits,dtRcpts"
																rendered="#{paymentController.visibilityFinder['Security Deposit,deposit']}"
																oncomplete="hideLoading(); if(! #{paymentController.visibilityFinder['Security Deposit,deposit']}) document.getElementById('secDepGrid').style.display='none';  if(#{paymentController.depositComplete}) { alert('Security Deposit and Utensil Deposit is complete for #{paymentController.admission.patient.name}!\nGoing to ID Generation Page...'); showLoading();document.location.href='generateId.jsf?id=#{paymentController.admission.id}';}"/>
												
												<a4j:commandButton onclick="if(document.getElementById('txtSecDep').value == 0) { alert('Amount is not valid !'); return false;} showLoading(); " 
																value="Refund" 
																id="btnSecDepRef" limitToList="true"
																action="#{paymentController.securityDepositRefund}" 
																reRender="dtSecurityDeposits,dtRcpts"
																rendered="#{paymentController.visibilityFinder['Security Deposit,refund']}"
																oncomplete="hideLoading(); if(! #{paymentController.visibilityFinder['Security Deposit,deposit']}) document.getElementById('secDepGrid').style.display='none'; if(#{paymentController.refundComplete}) { alert('Refund and Checkout for #{paymentController.admission.patient.name} is complete !\nGoing to Checkout page...'); showLoading();document.location.href='exit.jsf';}"/>	
											</h:panelGrid>																												
										
											<rich:dataTable value="#{paymentController.securityDeposits}" var="payment" id="dtSecurityDeposits">
												<f:facet name="header">
													<h:outputText value="Security Deposit"/>
												</f:facet>
												<rich:column>
													<f:facet name="header">Payment Date</f:facet>
													<h:outputText value="#{payment.receipt.date}">
														<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">Receipt</f:facet>
													<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
												</rich:column>
												<rich:column>
													<f:facet name="header">Amount</f:facet>
													<h:outputText value="#{payment.amount} " />
												</rich:column>
												<rich:column>
													<f:facet name="header">Type</f:facet>
													#{payment.refund ? 'Refund':'Deposit'}													
												</rich:column>
												<f:facet name="footer">
													<h:outputText rendered="#{empty paymentController.securityDeposits}" value="No deposits/refunds found !"/>
												</f:facet>																							
											</rich:dataTable>
								</h:panelGrid>								
							</rich:panel>
							
							
							
							<rich:panel rendered="#{paymentController.accessFinder['Utensil Deposit']}"  label="Utensil Deposit" switchType="client" >
								
								<h:panelGrid columns="2">
											<h:panelGrid columns="2" styleClass="noprint" id="utDepGrid"
											rendered="#{paymentController.visibilityFinder['Utensil Deposit,deposit'] or paymentController.visibilityFinder['Utensil Deposit,refund']}">
												<h:outputLabel value="Amount: "/>
												<h:inputText id="txtUteDepPay" 
															onkeyup="validateInteger(this,true,'Only numbers allowed !');" size="4" value="#{paymentController.utensilDepositAmount}"/>
																						
											<a4j:commandButton onclick="if(document.getElementById('txtUteDepPay').value == 0) { alert('Amount is not valid !'); return false;} showLoading();" 
																value="Deposit" action="#{paymentController.utensilDeposit}" 
																reRender="dtUtensilDeposits,dtRcpts" limitToList="true"
																rendered="#{paymentController.visibilityFinder['Utensil Deposit,deposit']}"
																oncomplete="hideLoading();if(! #{paymentController.visibilityFinder['Utensil Deposit,deposit']}) document.getElementById('utDepGrid').style.display='none';  if(#{paymentController.depositComplete}) { alert('Security Deposit and Utensil Deposit is complete for #{paymentController.admission.patient.name}!\nGoing to ID Generation Page...'); showLoading();document.location.href='generateId.jsf?id=#{paymentController.admission.id}'; }"
																/>
											<a4j:commandButton onclick="if(document.getElementById('txtUteDepPay').value == 0) { alert('Amount is not valid !'); return false;}showLoading(); " 
																value="Refund" action="#{paymentController.utensilDepositRefund}" 
																reRender="dtUtensilDeposits,dtRcpts" limitToList="true"
																rendered="#{paymentController.visibilityFinder['Utensil Deposit,refund']}"
																oncomplete="hideLoading();if(! #{paymentController.visibilityFinder['Utensil Deposit,refund']}) document.getElementById('utDepGrid').style.display='none';  if(#{paymentController.refundComplete}) { alert('Refund and Checkout for #{paymentController.admission.patient.name} is complete !\nGoing to Checkout page...'); showLoading();document.location.href='exit.jsf';}"/>
											</h:panelGrid>
										
											<rich:dataTable value="#{paymentController.utensilDeposits}" var="payment" id="dtUtensilDeposits">
												<f:facet name="header">
													<h:outputText value="Utensil Deposit"/>
												</f:facet>
												<rich:column>
													<f:facet name="header">Payment Date</f:facet>
													<h:outputText value="#{payment.receipt.date}">
														<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
													</h:outputText>
												</rich:column>									
												<rich:column>
													<f:facet name="header">Receipt</f:facet>
													<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{payment.receipt.id}')">#{payment.receipt.id}</a>
												</rich:column>
												<rich:column>
													<f:facet name="header">Amount</f:facet>
													<h:outputText value="#{payment.amount} " />
												</rich:column>
												<rich:column>
													<f:facet name="header">Type</f:facet>
													#{payment.refund ? 'Refund':'Deposit'}													
												</rich:column>
												<f:facet name="footer">
													<h:outputText rendered="#{empty paymentController.utensilDeposits}" value="No deposits/refunds found !"/>
												</f:facet>											
											</rich:dataTable>										
								</h:panelGrid>
							</rich:panel>
			
			</h:panelGrid>
				
				
						
				<h:outputText value="NOTE: New Receipt will be created automatically for new deposits or refunds !"/>
								
				<rich:panel style="border:0">
					<f:facet name="header">
						<h:outputText value="Receipts"/>
					</f:facet>
					<rich:dataTable value="#{paymentController.receiptsData}" var="aBean" 
											id="dtRcpts" style="display:#{paymentController.admission.id le 0 ? 'none':'block' }"
											rows="5">							
								<rich:column>
									<f:facet name="header">ID</f:facet>
									<a href="#" onclick="launchChildWindow('${request.contextPath}/pages/receipt.jsf?id=#{aBean.id}')">#{aBean.id}</a>
								</rich:column>
								<rich:column>
									<f:facet name="header">Date</f:facet>
									<h:outputText value="#{aBean.date}">
										<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
									</h:outputText>
								</rich:column>
								<rich:column>
									<f:facet name="header">Current Receipt</f:facet>
									<h:selectBooleanCheckbox value="#{aBean.selected}" disabled="true">
										<a4j:support event='onclick' action="#{paymentController.selectReceipt}" limitToList="true"
					   											reRender="dtRcpts,receiptInfo">
					   							</a4j:support>
									</h:selectBooleanCheckbox>									
								</rich:column>
								<f:facet name="footer">
									<rich:datascroller for="dtRcpts"/>
								</f:facet>
							</rich:dataTable>	
				</rich:panel>


		</h:form>
		
		
	<rich:modalPanel id="refundSecurityDeposit" width="450" zindex="2000" height="300" resizeable="false"  >
			<f:facet name="header">
				<h:outputText value="Refund Security Deposit"/>
			</f:facet>		
			<h:form>
				<table width="100%" align="left">					
					<tr>
						<td valign="top">
							<h:panelGrid columns="2">
								<h:outputLabel value="Amount"/>
								<h:inputText value="0"/>
							</h:panelGrid>							
						</td>
						<td valign="top">
							<h:panelGrid columns="1">
								<a4j:commandButton oncomplete="Richfaces.hideModalPanel('refundSecurityDeposit'); return false;" value="OK" />
								<a4j:commandButton onclick="Richfaces.hideModalPanel('refundSecurityDeposit'); return false;" value="Close"/>
							</h:panelGrid>						
						</td>
					</tr>
				</table>
			</h:form>
	</rich:modalPanel>	
	
	<rich:modalPanel id="depositSummaryPanel" width="450" zindex="2000" height="300" resizeable="false"  >
		<f:facet name="header">
			<h:outputText value="Deposit Summary"/>
		</f:facet>		
		<h:form>
			<rich:dataTable value="#{paymentController.deposits}" var="aBean">							
				<rich:column>
					<f:facet name="header">Receipt ID</f:facet>
					<h:outputText value="#{aBean.receipt.id}"/>
				</rich:column>
				<rich:column>
					<f:facet name="header">Date</f:facet>
					<h:outputText value="#{aBean.receipt.date}">
						<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
					</h:outputText>
				</rich:column>
				<rich:column >
					<f:facet name="header">Payments</f:facet>
					<h:outputText value="#{aBean.paymentType.name} Rs #{aBean.amount}"/>
				</rich:column>
				
				<f:facet name="footer">
					<h:outputText rendered="#{empty paymentController.deposits}" value="No deposits done"/>
				</f:facet>
			</rich:dataTable>					

			<h:panelGrid columns="2">
				<h:outputLabel value="Utensil Deposit Required"/>
				<h:outputText style="color:red" value="Rs #{paymentController.admission.materialSet.deposit} "/>
				<h:outputText value="Utensil Deposit done"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getUtensilDepositDone', paymentController.receipts)}"/>
				<h:outputText value="Security Deposit done"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getSecurityDepositDone', paymentController.receipts)}"/>
				<h:outputLabel value="Total Deposit received" />
				<h:outputText value="Rs #{fn:call(paymentController,'calculateReceiptTotalDeposit', paymentController.receipts)}"/>
			</h:panelGrid>
			<h:panelGrid columns="1">					
				<h:outputText value="Click OK to confirm that DEPOSIT is complete and proceed for ID Generation"/>
				<h:panelGroup>
					<a4j:commandButton action="#{paymentController.completeDeposit}" 
						oncomplete="document.location.href='generateId.jsf?id=#{paymentController.lastId}'" value="OK" />
					<rich:spacer width="25px" />
					<a4j:commandButton onclick="Richfaces.hideModalPanel('depositSummaryPanel'); return false;" value="Close"/>
				</h:panelGroup>
			</h:panelGrid>				
		</h:form>
	</rich:modalPanel>	
	
	<rich:modalPanel id="refundSummaryPanel" width="450" zindex="2000" height="300" resizeable="false"  >
		<f:facet name="header">
			<h:outputText value="Refund Summary"/>
		</f:facet>		
		<h:form>
			<rich:dataTable value="#{paymentController.refunds}" var="aBean">							
				<rich:column>
					<f:facet name="header">Receipt ID</f:facet>
					<h:outputText value="#{aBean.receipt.id}"/>
				</rich:column>
				<rich:column>
					<f:facet name="header">Date</f:facet>
					<h:outputText value="#{aBean.date}">
						<f:convertDateTime pattern="dd MMM yyyy HH:mm" timeZone="GMT+05:30"/>
					</h:outputText>
				</rich:column>
				<rich:column >
					<f:facet name="header">Summary</f:facet>
					<h:outputText value="#{aBean.paymentType.name}-Refund Rs #{aBean.amount}"/>
				</rich:column>
				<f:facet name="footer">
					<h:outputText rendered="#{empty paymentController.refunds}" value="No refunds done"/>
				</f:facet>
			</rich:dataTable>					

			<h:panelGrid columns="2">				
				<h:outputText value="Utensil Deposit received"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getUtensilDepositDone', paymentController.receipts)}"/>
				<h:outputText value="Utensil Deposit Refund done"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getUtensilRefundDone', paymentController.receipts)}"/>
				<h:outputText value="Security Deposit done"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getSecurityDepositDone', paymentController.receipts)}"/>
				<h:outputText value="Security Deposit Refund done"/>
				<h:outputText style="color:red" value="Rs #{fn:call(paymentController,'getSecurityRefundDone', paymentController.receipts)}"/>
				<h:outputLabel value="Total Deposit received" />
				<h:outputText value="Rs #{fn:call(paymentController,'calculateReceiptTotalDeposit', paymentController.receipts)}"/>
				<h:outputLabel value="Total Refund done" />
				<h:outputText value="Rs #{fn:call(paymentController,'calculateReceiptTotalRefund', paymentController.receipts)}"/>
			</h:panelGrid>
			<h:panelGrid columns="1">			
				<h:outputText value="Click OK to confirm that REFUND is complete and Checkout this patient"/>
				<h:panelGroup>
					<a4j:commandButton action="#{paymentController.completeRefund}" 
						oncomplete="document.location.href='exit.jsf'" value="OK" />
					<rich:spacer width="25px" />
					<a4j:commandButton onclick="Richfaces.hideModalPanel('refundSummaryPanel'); return false;" value="Close"/>
				</h:panelGroup>
			</h:panelGrid>				
		</h:form>
	</rich:modalPanel>	
		
	</ui:define>
</ui:composition>